home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 425 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.2 KB

  1. Path: ivan.iecc.com!not-for-mail
  2. From: stevec@pact.srf.ac.uk (Stephen Clarke)
  3. Newsgroups: comp.compilers,comp.std.c
  4. Subject: Re: Aliasing in ISO C
  5. Followup-To: comp.std.c
  6. Date: 14 Feb 1996 21:35:36 -0500
  7. Organization: University of Bristol, England
  8. Sender: johnl@iecc.com
  9. Approved: compilers@ivan.iecc.com
  10. Message-ID: <96-02-167@comp.compilers>
  11. References: <96-01-037@comp.compilers> <96-02-082@comp.compilers> <96-02-116@comp.compilers>
  12. NNTP-Posting-Host: localhost.iecc.com
  13. Keywords: C, optimize, standards
  14.  
  15. Ronald F. Guilmette (rfg@monkeys.com) wrote:
  16. : The C standard is pretty clear in saying that in the following
  17. : function, it is permissible to avoid the second indirection and load
  18. : from *p1...  assuming that you still have the value previously loaded
  19. : from *p1 in a register somewhere:
  20.  
  21. :     volatile int i;
  22. :     volatile char ch1, ch2;
  23.  
  24. :     void foobar (char *p1, int *p2)
  25. :     {
  26. :         ch1 = *p1;
  27. :         *p2 = i;
  28. :         ch2 = *p1;
  29. :     }
  30.  
  31. : In other words, the C standard is pretty clear in saying that C only
  32. : supports aliasing of (i.e. multiple pointers to) ``compatible'' (which
  33. : in practice usually means ``identical'') types.  In other cases, the C
  34. : standard makes few guarantees.
  35.  
  36. If I understand correctly, the part of the standard you are referring
  37. to is 6.3 Expressions, where rules are given for the type of the
  38. lvalue used to access an object's stored value ... but this section
  39. allows any object to be accessed using a character type, so it seems
  40. to me that *p1 could access an object of any type, including int,
  41. which means that *p1 could access the same object as *p2.
  42.  
  43. This is just an unfortunate choice of types in the example.  Given
  44.  
  45.     volatile int i;
  46.     volatile long la, lb;
  47.  
  48.     void foobar (long *p1, int *p2)
  49.     {
  50.       la = *p1;
  51.       *p2 = i;
  52.       lb = *p1;
  53.     }
  54.  
  55. I agree with you, though I've never been brave enough take advantage
  56. of these aliasing restrictions in the compiler I work on!
  57.  
  58. Steve.
  59. (Follow-ups to comp.std.c.)
  60. --
  61. --------------
  62. Stephen Clarke,
  63. PACT (Partnership in Advanced Computer Technologies)
  64. Email: stevec@pact.srf.ac.uk
  65. Mail : 10 Priory Road, Clifton, Bristol, BS8 1TU. UK.
  66. Phone: +44 117 970 7188, Fax +44 272 707 171.
  67. --
  68. Send compilers articles to compilers@iecc.com,
  69. meta-mail to compilers-request@iecc.com.
  70.